﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using TaoBaoProject.Common;
using Model;
using System.Data;
using System.Web.Security;

namespace Web
{
  public partial class Login : System.Web.UI.Page
  {
    protected static readonly log4net.ILog log =
    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    protected void Page_Load(object sender, EventArgs e)
    {
      ImgBtnLogon.Attributes.Add("onmouseover", "MM_swapImage('ImgBtnLogon','','../Images/Logon/login_09_1.gif',1)");
      ImgBtnLogon.Attributes.Add("onmouseout", "MM_swapImgRestore()");

      ImgBtnReset.Attributes.Add("onmouseover", "MM_swapImage('ImgBtnReset','','../Images/Logon/login_10_1.gif',1)");
      ImgBtnReset.Attributes.Add("onmouseout", "MM_swapImgRestore()");

    }

    protected void ImgBtnLogon_Click(object sender, ImageClickEventArgs e)
    {
      string username = this.txtUserName.Text;
      string password = this.txtPassword.Text;
      DataTable dt;
      try
      {
        dt = new DataTable();
        dt = UserBLL.LoginUser(username, password);
        if (dt.Rows.Count == 1)
        {
          Users user = new Users();
          user.IsAdmin = Convert.ToByte(dt.Rows[0]["IsAdmin"]);
          user.UserName = username;
          user.ContainFileds = dt.Rows[0]["ContainsProFileds"].ToString();
          ContextWeb.CurrentUser = user;
          FormsAuthentication.RedirectFromLoginPage(username, true);
          log.Info("用户【" + username + "】在" + DateTime.Now.ToString("F") + " 登录成功，");
        }
        else
        {
          msgError.Text = "用户名或密码错误！";
          log.Info("用户【" + username + "】在" + DateTime.Now.ToString("F") + " 密码输入错误。");
        }
      }
      catch (Exception ex)
      {
        log.Info("用户【" + username + "】在" + DateTime.Now.ToString("F") + " 登录时候发生了异常。异常信息是： " + ex.Message);
        throw;
      }

    }

    protected void ImgBtnReset_Click(object sender, ImageClickEventArgs e)
    {
      txtUserName.Text = "";
      txtPassword.Text = "";
      log.Info(DateTime.Now.ToString("F") + " 重置了用户名和密码。");
    }
  }
}